﻿using Common;
using DevExpress.XtraEditors;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class ChooseOutPatiens : Form
    {
        public NursingFormSystem.DNNursingService.BNNursingDocsSystem webserver = new NursingFormSystem.DNNursingService.BNNursingDocsSystem();
        public static Tb_User LoginUser;
        public  string PatientID = "";
        public  string VisitID = "";
        public string PatientName = "";
        public struct ReturnPatientEntity
        {
            /// <summary>
            /// 住院号
            /// </summary>
            public static string PatientID;
            /// <summary>
            ///  患者名称
            /// </summary>
            public static string PatientName;
            /// <summary>
            /// 住院次数
            /// </summary>
            public static string VisitID;
            /// <summary>
            /// 床号
            /// </summary>
            public static string BedNo;
            /// <summary>
            /// 性别
            /// </summary>
            public static string Sex;
            /// <summary>
            /// 入院日期
            /// </summary>
            public static string InDate;
        };  
        public ChooseOutPatiens(string[] UserParam)
        {
            this.UserParam = UserParam;
            InitLoginUser(UserParam);
            InitializeComponent();
            gridControl1.DoubleClick+=gridControl1_DoubleClick;
           
        }
        public Tb_User InitLoginUser(string[] UserParam)
        {
            //if (LoginUser == null)
            //{
                LoginUser = new Tb_User();
                LoginUser.UserId = UserParam[0];
                LoginUser.UserName = UserParam[1];
                LoginUser.DepartmentCode = UserParam[2];
                LoginUser.DepartmentName = UserParam[3];
                LoginUser.JobTitle = UserParam[4];
                LoginUser.PositionTitle = UserParam[5];
            //}
            return LoginUser;
        }
        public string[] UserParam { get; set; }
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            submitValue();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if(text1.Text.Length == 0)
            {
                XtraMessageBox.Show("请输入患者姓名或住院号！");
                return;
            }

            NursingFormSystem.DNNursingService.ResultHelperOfDataTable x = webserver.GetPatientHistory(LoginUser.DepartmentCode, text1.Text.Trim());
            if(x.S == true)
            {
                gridControl1.DataSource = UpdateDataTable(x.D);
            }
            else
            {
                XtraMessageBox.Show("查询失败！");
                return;
            }
             

            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsCustomization.AllowSort = false;
            gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;   //是否显示过滤面板
            gridView1.OptionsCustomization.AllowFilter = false;                      //是否允许过滤
            gridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView1.OptionsMenu.EnableColumnMenu = false;
            gridView1.OptionsView.ShowGroupPanel = false;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.Columns[0].Width = 100;
            gridView1.Columns[1].Width = 80;
            gridView1.Columns[2].Width = 100;
            gridView1.Columns[3].Width = 100;
            gridView1.Columns[4].Width = 100;
            gridView1.Columns[5].Width = 40;
            gridView1.Columns[6].Width = 40;
            gridView1.Columns[7].Width = 40;
            gridView1.Columns[8].Width = 200;
            gridView1.Columns[9].Width = 200;
            gridView1.Columns[10].Width = 200;
            gridView1.Columns[11].Width = 100;
            gridView1.Columns[0].Caption = "住院号";         
            gridView1.Columns[1].Caption = "住院次数";
            gridView1.Columns[1].Visible = false;
            gridView1.Columns[2].Caption = "姓名";    
            gridView1.Columns[3].Caption = "科室";    
            gridView1.Columns[4].Caption = "病区";    
            gridView1.Columns[5].Caption = "床号";    
            gridView1.Columns[6].Caption = "性别";    
            gridView1.Columns[7].Caption = "年龄";    
            gridView1.Columns[8].Caption = "入院日期";    
            gridView1.Columns[9].Caption = "出院日期";
            gridView1.Columns[10].Caption = "诊断";
            gridView1.Columns[11].Caption = "住院天数";
        }
        #region 修改datatable的datatype，让其可以显示完整时间

        /// <summary>
        ///当DataTable中有值时，是不允许修改列的DataType
        /// 修改数据表DataTable某一列的数据类型和记录值
        /// </summary>
        /// <param name="argDataTable">数据表DataTable</param>
        /// <returns>数据表DataTable</returns>
        private DataTable UpdateDataTable(DataTable argDataTable)
        {

            DataTable dtResult = new DataTable(); //克隆表结构
            dtResult = argDataTable.Clone();

            //修改数据列类型

            foreach (DataColumn col in dtResult.Columns)
            {

                if (col.ColumnName == "InDepartmentDate" || col.ColumnName == "OutDate")
                {
                    col.DataType = typeof(string);
                }

            }

            foreach (DataRow row in argDataTable.Rows)
            {
                dtResult.ImportRow(row);
            }

            return dtResult;

        }
        #endregion
        private void gridControl1_MouseDown(object sender, MouseEventArgs e)
        {
            //DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));
            //if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 2)
            //{
            //    if (hInfo.InRow)//判断光标是否在行范围内 
            //    {
            //        DialogResult = DialogResult.Yes;
            //        PatientID = gridView1.GetDataRow(hInfo.RowHandle)[0].ToString();
            //        VisitID = gridView1.GetDataRow(hInfo.RowHandle)[1].ToString();
            //        Close();
            //    }
            //}
        }

        private void text1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                simpleButton1_Click(null, null);
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {

            submitValue();
        }

        /// <summary>
        /// 提交返回数据
        /// </summary>
        private void submitValue()
        {
            try
            {
                if (gridView1.SelectedRowsCount == 0)
                    return;
                WriteLog.WriteLogString("双击事件！");
                PatientID = gridView1.GetFocusedDataRow().ItemArray[0].ToString();
                VisitID = gridView1.GetFocusedDataRow().ItemArray[1].ToString();
                //PatientName = gridView1.GetFocusedDataRow().ItemArray[2].ToString();

                ReturnPatientEntity.PatientName = gridView1.GetFocusedDataRow().ItemArray[2].ToString();
                ReturnPatientEntity.BedNo = gridView1.GetFocusedDataRow().ItemArray[5].ToString();
                ReturnPatientEntity.PatientID = gridView1.GetFocusedDataRow().ItemArray[0].ToString();
                ReturnPatientEntity.Sex = gridView1.GetFocusedDataRow().ItemArray[6].ToString();
                ReturnPatientEntity.InDate = gridView1.GetFocusedDataRow().ItemArray[8].ToString();
                DialogResult = DialogResult.Yes;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                WriteLog.WriteLogString(ex.ToString());
            }
        }

        private void ChooseOutPatiens_Load(object sender, EventArgs e)
        {
         
        }
    }
}
